package socket.myRPC.rpc;

import socket.myRPC.util.PackageMsg;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class ResponseMappingCallback {
  private final static ConcurrentHashMap<Long, CompletableFuture> mapping = new ConcurrentHashMap<>();
  
  public static void addCallBack(long requestID, CompletableFuture future) {
    mapping.put(requestID, future);
  }
  
  public static void runCallBack(PackageMsg msg) {
    long requestID = msg.getHeader().getRequestID();
    
    CompletableFuture completedFuture = mapping.get(requestID);
    completedFuture.complete(msg.getContent().getRes());
    remove(requestID);
  }
  
  public static void remove(long requestID) {
    mapping.remove(requestID);
  }
}
